//函数节流
export function throttle(func, delay) {
  var context, args;
  var previous = 0;
  return function() {
    var now = +new Date();
    context = this;
    args = arguments;
    if (now - previous > delay) {
      func.apply(context, args);
      previous = now;
    }
  };
}
//函数节流1(最后会再执行一次)
export function throttle1(fn, threshhold) {
  var timeout;
  var start = new Date();
  threshhold = threshhold || 160;
  return function() {
    var context = this,
      args = arguments,
      curr = new Date() - 0;

    clearTimeout(timeout); //总是干掉事件回调
    if (curr - start >= threshhold) {
      // console.log("now", curr, curr - start); //注意这里相减的结果，都差不多是160左右
      fn.apply(context, args); //只执行一部分方法，这些方法是在某个时间段内执行一次
      start = curr;
    } else {
      //让方法在脱离事件后也能执行一次
      timeout = setTimeout(function() {
        fn.apply(context, args);
      }, threshhold);
    }
  };
}
//函数防抖
export function debounce(func, delay) {
  var timeout;
  return function() {
    clearTimeout(timeout);
    var context = this,
      args = arguments;
    timeout = setTimeout(function() {
      func.apply(context, args);
    }, delay);
  };
}
